﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Nova_DAL;
using Nova_Common;
using System.Data.Common;
using System.Data;
//xgw 组盘解盘进行修改，去除了与入库明细表中的实际数量、组盘关系；只是组盘物料来源于入库单，组盘数量小于入库单计划数量；

public partial class Manage_Package_Un : System.Web.UI.Page
{
    DbHelper dbHelper = new DbHelper();
    DbCommand cmd = null;
    DataSet ds = null;
    string strSQL = "";
    int iResNo = 0;
    string strResMsg = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

        }
    }

    protected void ibtn_JP_Click(object sender, ImageClickEventArgs e)
    {
        for (int i = 0; i < gv.Rows.Count; i++)
        {
            CheckBox chkSelect = (CheckBox)gv.Rows[i].Cells[0].FindControl("chkSelect");
            if (chkSelect.Checked)
            {
                cmd = dbHelper.GetStoredProcCommond("[SP_UNPACKAGE_ALL_0000]");
                dbHelper.AddInParameter(cmd, "@ContainerID", DbType.String, gv.Rows[i].Cells[6].Text);
                dbHelper.AddInParameter(cmd, "@SEC_MAT_ID", DbType.String, gv.Rows[i].Cells[7].Text);
                dbHelper.AddInParameter(cmd, "@MAT_NUM", DbType.Decimal, gv.Rows[i].Cells[5].Text);
                dbHelper.AddInParameter(cmd, "@USERNAME", DbType.String, Request.Cookies["vNovaUserNo"].Value.ToString());
                dbHelper.AddOutParameter(cmd, "@RESNO", DbType.Int32, 100);
                dbHelper.AddOutParameter(cmd, "@RESMSG", DbType.String, 255);
                dbHelper.ExecuteNonQuery(cmd);

                iResNo = Convert.ToInt32(dbHelper.GetParameter(cmd, "@RESNO").Value.ToString());
                strResMsg = dbHelper.GetParameter(cmd, "@RESMsg").Value.ToString();
                if (iResNo <= 0)
                {
                    lab_Msg.Text = "解盘失败！" + strResMsg.ToString();
                    return;
                }
                else
                {   //xgw 修改解盘成功后入库单明细实际数据的修改
                    //strSQL = @"UPDATE TAddStorage_List SET ACT_NUM=0 WHERE STOCKNO=@STOCKNO AND SEC_MAT_ID=@SEC_MAT_ID";
                    //cmd = dbHelper.GetSqlStringCommond(strSQL);
                    //dbHelper.AddInParameter(cmd, "@STOCKNO", DbType.String, gv.Rows[i].Cells[8].Text.Trim());
                    //dbHelper.AddInParameter(cmd, "@SEC_MAT_ID", DbType.String, gv.Rows[i].Cells[7].Text);
                    //dbHelper.ExecuteNonQuery(cmd);


                    lab_Msg.Text = "解盘成功！";

                   //xgw 解盘成功后入库单明细实际数据的修改
                    

                    ////strSQL = @"SELECT *  FROM TPackage_tail where TrayID=@TrayID and Sec_Mat_ID=@Sec_Mat_ID";
                    //cmd = dbHelper.GetSqlStringCommond(strSQL);
                    //dbHelper.AddInParameter(cmd, "@TrayID", DbType.String, gv.Rows[i].Cells[1].Text.Trim());
                    //dbHelper.AddInParameter(cmd, "@Sec_Mat_ID", DbType.Int64, Convert.ToInt64(gv.Rows[i].Cells[7].Text.Trim()));
                    //DataSet ds = dbHelper.ExecuteDataSet(cmd);
                    //if (ds != null && ds.Tables[0].Rows.Count > 0)
                    //{
                    //    for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                    //    {
                    //        strSQL = @"UPDATE TAddStorage_List SET ACT_NUM=ACT_NUM-@Num where StockNo=@StockNo and Sec_Mat_ID=@Sec_Mat_ID";
                    //        cmd = dbHelper.GetSqlStringCommond(strSQL);
                    //        dbHelper.AddInParameter(cmd, "@STOCKNO", DbType.String, ds.Tables[0].Rows[j]["StockNo"].ToString());
                    //        dbHelper.AddInParameter(cmd, "@SEC_MAT_ID", DbType.Int64,Convert.ToInt64(ds.Tables[0].Rows[j]["SEC_MAT_ID"]));
                    //        dbHelper.AddInParameter(cmd, "@Num", DbType.Decimal, Convert.ToDecimal(ds.Tables[0].Rows[j]["Num"]));
                    //        dbHelper.ExecuteNonQuery(cmd);
                    //    }
                    //}
                    ////删除组盘明细表相关的数据项
                    //strSQL = @"delete FROM TPackage_tail where TrayID=@TrayID and Sec_Mat_ID=@Sec_Mat_ID";

                   
                    //cmd = dbHelper.GetSqlStringCommond(strSQL);
                    //dbHelper.AddInParameter(cmd, "@TrayID", DbType.String, gv.Rows[i].Cells[1].Text.Trim());
                    //dbHelper.AddInParameter(cmd, "@Sec_Mat_ID", DbType.Int64, Convert.ToInt64(gv.Rows[i].Cells[7].Text.Trim()));
                    //dbHelper.ExecuteNonQuery(cmd);

                }
            }
        }
        ibtn_Search_Click(sender, e);
    }

    protected void ibtn_Search_Click(object sender, ImageClickEventArgs e)
    {

        //HttpCookie WType = Request.Cookies["WType"];
        string strTray_ID = "H" + ddl_Zone.SelectedValue.ToString() + ddl_Dou.SelectedValue.ToString();
        
// xgw 修改查询显示 （显示的内容为组盘关系，不再显示与入库单的关系）     
//        strSQL = @"SELECT TAL.STOCKNO,TRP.TRAY_ID,TRT.RELATIONS_ID,TBS.SEC_MAT_ID,TBI.MAT_CODE,TBM.S01,TBM.MAT_NAME,TBS.Mat_Num,TAL.NUM,TAL.ACT_NUM,TBS.CONTAINERID FROM TRelations TRP 
//	                    INNER JOIN TBase_Position TBP ON TRP.POS_ID=TBP.POS_ID
//	                    INNER JOIN TBase_Zone TBZ ON TBZ.ZONE_ID=TBP.ZONE_ID
//	                    INNER JOIN TBase_TrayPos TBT ON TBT.TRAY_ID = TRP.TRAY_ID
//	                    INNER JOIN TRel_TrayPosContainer TRT ON TRT.RELATIONS_ID=TBT.RELATIONS_ID
//	                    INNER JOIN TBase_Storage TBS ON TBS.CONTAINERID=TRT.CONTAINERID
//	                    INNER JOIN TBase_Interface TBI ON TBI.SEC_MAT_ID=TBS.SEC_MAT_ID
//	                    INNER JOIN TBase_Material TBM ON TBM.MAT_CODE=TBI.MAT_CODE
//                        INNER JOIN TAddStorage_List TAL ON TAL.TS01=TBS.PID
//	                    WHERE TBZ.STRUCTURE<>1 AND TRP.TRAY_ID<>'200000000000' AND (TBI.S01 = '" + WType.Value.ToString().Trim() + "' OR 'FSSC'='" + WType.Value.ToString().Trim() + "')";
        strSQL = @"SELECT TRP.TRAY_ID,TRT.RELATIONS_ID,TBS.SEC_MAT_ID,TBI.MAT_CODE,TBM.S01,TBM.MAT_NAME,TBS.Mat_Num,TBS.CONTAINERID FROM TRelations TRP 
	                    INNER JOIN TBase_Position TBP ON TRP.POS_ID=TBP.POS_ID
	                    INNER JOIN TBase_Zone TBZ ON TBZ.ZONE_ID=TBP.ZONE_ID
	                    INNER JOIN TBase_TrayPos TBT ON TBT.TRAY_ID = TRP.TRAY_ID
	                    INNER JOIN TRel_TrayPosContainer TRT ON TRT.RELATIONS_ID=TBT.RELATIONS_ID
	                    INNER JOIN TBase_Storage TBS ON TBS.CONTAINERID=TRT.CONTAINERID
	                    INNER JOIN TBase_Interface TBI ON TBI.SEC_MAT_ID=TBS.SEC_MAT_ID
	                    INNER JOIN TBase_Material TBM ON TBM.MAT_CODE=TBI.MAT_CODE
	                    WHERE TBZ.STRUCTURE<>1 AND TRP.TRAY_ID<>'200000000000' ";


        if (rbtnl_Zone.SelectedValue == "1")
        {
            if (txt_TRAY_ID.Text.Trim() != "")
            {
                strSQL += " AND TRP.TRAY_ID = '" + txt_TRAY_ID.Text.Trim() + "'";
            }
        }
        else
        {
            strSQL += " AND TRP.TRAY_ID = '" + strTray_ID + "'";
        }
        strSQL += "  ORDER BY TRT.RELATIONS_ID ";
        cmd = dbHelper.GetSqlStringCommond(strSQL);
        gv.DataSource = dbHelper.ExecuteDataSet(cmd).Tables[0].DefaultView;
        gv.DataBind();
    }

    protected void rbtnl_Zone_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (rbtnl_Zone.SelectedValue == "1")
        {
            td_t.Visible = true;
            td_h.Visible = false;
        }
        else
        {
            td_t.Visible = false;
            td_h.Visible = true;
        }
    }
}
